<?php
require_once 'AbstractModel.class.php';
/**
 * Album picasa.
 * 
 * @author julien
 */
class Album extends AbstractModel {
                      
    public $id;
    public $title;
    public $thumbnailUrl;
    public $albumName;
    public $username;
    public $url;
    public $rights;
    public $numPhotos;
    public $description;
    public $date;          
    
    // photos de l'album
    //private $photos = array();
    
    public $albumEntry;
    
    public static function createAlbum($entry) {
        $album = new Album();
        
        $album->albumEntry = $entry;
        $album->title = $entry->getTitle()->getText();
        $album->thumbnailUrl = $entry->getMediaGroup()->thumbnail[0]->getUrl();
        $album->albumName = $entry->getGphotoName()->getText();
        $album->id = $entry->getGphotoId()->getText();
        $album->username = $entry->getGphotoUser()->getText();
        $album->rights = $entry->getRights()->getText();
        $album->numPhotos = $entry->getGphotoNumPhotos()->getText();
        $album->description = $entry->getSummary()->getText();
        $album->url = $entry->link[1]->getHref();
        $album->date = strtotime($entry->getPublished()->getText()); 
        
        return $album;
    }
    
    public function setTableDefinition() {           
        $this->hasColumn('id', 'integer', 4, array("primary" => true));     
        $this->hasColumn('albumName', 'string', 255, array("notnull" => true));
        $this->hasColumn('username', 'string', 50, array("notnull" => true));
        $this->hasColumn('title', 'string', 255, array("notnull" => true));  
        $this->hasColumn('thumbnailUrl', 'string', 255, array("notnull" => true));
        $this->hasColumn('url', 'string', 255, array("notnull" => true));                      
        $this->hasColumn('rights', 'string', 10, array("notnull" => true));
        $this->hasColumn('numPhotos', 'integer', 1, array("notnull" => true));
        $this->hasColumn('description', 'string', 255);   
        $this->hasColumn('date', 'date', array("notnull" => true)); 
        //$this->hasColumn('photos', 'date', array("notnull" => true));
    }
    
    public function setUp() {
        $this->hasMany('Photo as photos', array(
                'refClass' => 'Photo',
                'local' => 'id',
                'foreign' => 'id'
            )
        );
    }     

    public function getId() {
        return $this->id;
    }

    public function getAlbumId() {
        return $this->id;
    }

    public function getTitle() {
        return $this->title;
    }
    
    public function setTitle($newTitle) {
        $this->albumEntry->title->text = $newTitle;
        $this->albumEntry = $this->albumEntry->save();
    }

    public function getThumbnailUrl() {
        return $this->thumbnailUrl;
    }
    
    public function setThumbnailUrl($url) {
        $this->albumEntry->mediaGroup->thumbnail[0]->url = $url;
        $this->albumEntry = $this->albumEntry->save();
    }

    public function getUrl() {
        return $this->url;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getNumPhotos() {
        return $this->numPhotos;
    }
    
    public function getDate() {
        return $this->date;
    }
    
    /**
     * Permet le trie des albums par date.
     */     
    public static function compareDate($a1, $a2) {
        if($a1->getDate() == $a2->getDate()) return 0 ; 
        return ($a1->getDate() > $a2->getDate()) ? -1 : 1;
    }
             
}
?>
